import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-07-18
 * Time: 11:24
 */
public class ShellSort {//升序排序
    void swap(int a,int b,int[] array){
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }

    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j;
            for (j = i-1; j >= 0; j--) {
                if (array[j] > tmp) array[j + 1] = array[j];
                else break;
            }
            array[j+1] = tmp;
        }
    }

    private static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j;
            for (j = i-gap; j >= 0; j-=gap) {
                if (array[j] > tmp) array[j + gap] = array[j];
                else break;
            }
            array[j+gap] = tmp;
        }
    }

    public static void shellSort(int[] array){
        for(int i=array.length-1;i>=1;i--){
            shell(array,i);
        }
    }

    public static void main(String[] args) {
        int[] arr = {4,7,2,7,3,9,2,10,11,13};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }

}
